﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Shared;

namespace BobTest
{
    public class BobControl
    {
        public static bool InitBobReg(Shared.Method.YcdTelnetSecond telnet)
        {
            //bool writeRegister = Shared.Method.BobBase.WriteReg(telnet, 0x51, 0x7b,0x7f,new byte[] { 0x12,0x23,0x58,0x13,0x03});
            bool writeRegister = Shared.Method.BobBase.WriteReg(telnet, 0x51, 0x7F, 0x03);
            if (writeRegister == false) return false;

            //开启APC
            byte bRegister = Shared.Method.BobBase.ReadReg(telnet, 0x51, 0x8A);
            byte bChange = Shared.Method.BobBase.ChangeBit(bRegister, 2, true);//开启APC
            bChange = Shared.Method.BobBase.ChangeBit(bChange, 7, false);//LDD突发模式
            writeRegister = Shared.Method.BobBase.WriteReg(telnet, 0x51, 0x8A, bChange);

            if (writeRegister == false) return false;

            //开启单闭环模式
            bRegister = Shared.Method.BobBase.ReadReg(telnet, 0x51, 0x82);
            bChange = Shared.Method.BobBase.ChangeBit(bRegister, 3, true);
            writeRegister = Shared.Method.BobBase.WriteReg(telnet, 0x51, 0x82, bChange);

            if (writeRegister == false) return false;

            //MD_ADCT_EN 不使能
            writeRegister = Shared.Method.BobBase.WriteReg(telnet, 0x51, 0x89, 0xC0);
            if (writeRegister == false) return false;
            //register 141-143
            writeRegister = Shared.Method.BobBase.WriteReg(telnet, 0x51, 0x8D, 0x8F, new byte[] { 0x60, 0x40, 0x88 });
            if (writeRegister == false) return false;

            //145
            writeRegister = Shared.Method.BobBase.WriteReg(telnet, 0x51, 0x91, 0xC8);
            if (writeRegister == false) return false;

            //148
            writeRegister = Shared.Method.BobBase.WriteReg(telnet, 0x51, 0x94, 0x09);
            if (writeRegister == false) return false;

            //135-136
            byte[] registers = Shared.Method.BobBase.Int2Bytes(100, true);
            writeRegister = Shared.Method.BobBase.WriteReg(telnet, 0x51, 0x87, 0x88, registers);
            if (writeRegister == false) return false;

            return true;
        }

        public static bool CheckSum(Shared.Method.YcdTelnetSecond telnet)
        {
            //Table4/5/6 128-232,Table3 130-210
            DateTime startTime = DateTime.Now;

            //change table to 4
            Shared.Method.BobBase.WriteReg(telnet, 0x51, 0x7f, 0x03);
            byte[] table3 = Shared.Method.BobBase.ReadReg(telnet, 0x51, 130, 210);

            Shared.Method.BobBase.WriteReg(telnet, 0x51, 0x7f, 0x04);
            byte[] table4 = Shared.Method.BobBase.ReadReg(telnet, 0x51, 128, 232);

            Shared.Method.BobBase.WriteReg(telnet, 0x51, 0x7f, 0x05);
            byte[] table5 = Shared.Method.BobBase.ReadReg(telnet, 0x51, 128, 232);

            Shared.Method.BobBase.WriteReg(telnet, 0x51, 0x7f, 0x06);
            byte[] table6 = Shared.Method.BobBase.ReadReg(telnet, 0x51, 128, 232);

            //calculation check sum
            int sum = 0;
            for(int i = 0; i < table3.Length; i++)
            {
                sum += table3[i];
            }

            for(int j = 0; j < table4.Length; j++)
            {
                sum += table4[j];
                sum += table5[j];
                sum += table6[j];
            }

            bool writeCheckSum = false;

            if (sum > 0)
            {
                //write register 219
                writeCheckSum = Shared.Method.BobBase.WriteReg(telnet, 0x51, 219, (byte)sum);
            }

            double runseconds = (DateTime.Now - startTime).TotalSeconds;

            return writeCheckSum;
        }
    }
}
